package com.itfreer.data.general;

import com.itfreer.utils.StringUtils;

import java.util.HashMap;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * 定义更新对象
 */
public class UpdateMap implements IUpdateMap {

	private HashMap<String, Object> setMap = new HashMap<String, Object>();
	private HashMap<String, Object> whereMap = new HashMap<String, Object>();

	@Override
	public void setSetMap(HashMap<String, Object> setMap) {
		this.setMap = setMap;
	}

	@Override
	public HashMap<String, Object> getSetMap() {
		return setMap;
	}

	@Override
	public HashMap<String, Object> getWhereMap() {
		return whereMap;
	}

	@Override
	public void setWhereMap(HashMap<String, Object> whereMap) {
		this.whereMap = whereMap;
	}
	
	/**
	 * 获取SQL
	 * @param owerTable
	 * @return
	 */
	public WherePara getSql(ITable owerTable){
		WherePara para = new WherePara();
		// 查看是否为Oracle
		boolean isOracleWorksapce = false;
		String setString = "";
		Set<String> keys = setMap.keySet();
		for(String key : keys){
			setString+= key + "=@"+key+",";
			para.whereParas.add(new GlobalParameter("@"+key, setMap.get(key)));
		}
		if(setString!=""){
			setString = setString.substring(0, setString.length()-1);
		}
		if (whereMap != null) {
			para.whereString = QueryMap.getWhere(owerTable, whereMap, para.whereParas, isOracleWorksapce,
					true);
		}
		String sql = "Update " + owerTable.getTableName() + " set " + setString;
		if (!StringUtils.IsNullOrSpace(para.whereString))
			sql += " where " + para.whereString;
		para.sql = sql;
		return para;
	}
}